﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MaxLeaf.DB;
using MaxLeaf.Common;
using System.Data.SqlClient;

namespace MaxLeaf.DAL.W2K
{
    public class t_ItemDAL
    {

        public t_ItemDAL()
            : base()
        {
            
        }
        public string GetFItemId(DB.DBAccess dbDestK3Con, string strFItemClassID, int intFLevel, string strFParentID, string strFNumber,string strFName, int intFDetail)
        {
            string strSql=string.Empty;
            DataTable dtData = dbDestK3Con.GetTableData("t_item", new Dictionary<string, object>() { { "FItemClassID", strFItemClassID }, { "FNumber", strFNumber } }, "FitemID");
            if (dtData.Rows.Count == 0)
            {
                strSql = @"INSERT INTO t_Item (FItemClassID,FParentID,FLevel,FName,FNumber,FShortNumber,FFullNumber,FDetail,UUID,FDeleted)
                    values(@FItemClassID,@FParentID,@FLevel,@FName,@FNumber,@FShortNumber,@FFullNumber,@FDetail,@UUID,@FDeleted)
                ";
                dbDestK3Con.ExecuteSql(strSql, new SqlParameter[] { 
                    new SqlParameter("@FItemClassID",strFItemClassID)
                    ,new SqlParameter("@FParentID",strFParentID)
                    ,new SqlParameter("@FLevel",intFLevel.ToString())
                    ,new SqlParameter("@FName",strFName)
                    ,new SqlParameter("@FNumber",strFNumber)
                    ,new SqlParameter("@FShortNumber",GetShortNumber(strFNumber))
                    ,new SqlParameter("@FFullNumber",strFNumber)
                    ,new SqlParameter("@FDetail",intFDetail.ToString())
                    ,new SqlParameter("@UUID",Guid.NewGuid().ToString())
                    ,new SqlParameter("@FDeleted","0")
                });

                dtData = dbDestK3Con.GetTableData("t_item", new Dictionary<string, object>() { { "FItemClassID", strFItemClassID }, { "FNumber", strFNumber } }, "FitemID");
                
            }
            else
            {
                strSql = @"update  t_Item set FName=@FName,FNumber=@FNumber,FShortNumber=@FShortNumber,FFullNumber=@FFullNumber
                    where FitemID=@FitemID  and FItemClassID=@FItemClassID
                ";
                dbDestK3Con.ExecuteSql(strSql, new SqlParameter[] { 
                    new SqlParameter("@FName",strFName)
                    ,new SqlParameter("@FNumber",strFNumber)
                    ,new SqlParameter("@FShortNumber",GetShortNumber(strFNumber))
                    ,new SqlParameter("@FFullNumber",strFNumber)                    
                    ,new SqlParameter("@FitemID",dtData.Rows[0]["FitemID"])
                    , new SqlParameter("@FItemClassID", strFItemClassID)
                });
            }
            return CvtFunc.ToString(dtData.Rows[0]["FitemID"]);

            
        }

        private string  GetShortNumber(string strFNumber)
        {
            string[] arr = strFNumber.Split('.');
            return arr[arr.Length - 1];
        }

    }
}
